package alquilers;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.DataInputStream;



public class AlquilersImpl {
	public double addEntry(String model,String motor, String quantitat,String dies,String descompte){
		/*HashMap<String, Double> priceInfo = new HashMap<String, Double>();
        priceInfo.put("Economic", 200.0);
        priceInfo.put("Semi-Luxe", 400.0);
        priceInfo.put("Luxe", 699.95);
        priceInfo.put("Limusina", 1234.5);*/

		// Calcular preu
        double price;
        if (model.equals("Economic")) price = Double.parseDouble("200.0");
        else if (model.equals("Semi-Luxe")) price = Double.parseDouble("400.0");
        else if (model.equals("Luxe")) price = Double.parseDouble("699.95");
        else if (model.equals("Limusina")) price = Double.parseDouble("1234.5");
        else price = Double.parseDouble("299.95"); 
        price *= motor.equals("Gasolina") ? Double.parseDouble("1.1") : Double.parseDouble("0.9");
        price *= Integer.parseInt(dies);
        price *= Integer.parseInt(quantitat);
        price -= price * (Double.parseDouble(descompte) / 100);

        // Guardar a la 'base de dades'
        BufferedWriter fout;
        try {
                fout = new BufferedWriter(new FileWriter("lloguers.txt", true));
                fout.write(model + " " + motor + " "
                                + dies + " " + quantitat + " "
                                + descompte + " " + price);
                fout.newLine();
                fout.close();
        } catch (Exception e) {
                System.out.println("addEntry error: " + e.getMessage());
        }
        return price;

	}
	
	public String[] listado(String username, String password){
		
		if (username.equals("admin") && password.equals("password")) {
            String[] aux = new String[20*6];    //limit de 20 alquilers
            BufferedReader br = null;
            try {
                    br = new BufferedReader(
                                    new InputStreamReader(new DataInputStream(
                                                    new FileInputStream("lloguers.txt"))));
            } catch (Exception e) {
                    System.out.println("getTransactions error: " + e.getMessage());
            }
            String strLine = null;
            try {
                    strLine = br.readLine();
            } catch (Exception e) {
                    System.out.println("Readline null");
            }
            int i=0;
            while (strLine != null) {
                    String[] words = strLine.split(" ");
                    aux[i]=words[0];
                    aux[i+1]=words[1];
                    aux[i+2]=words[2];
                    aux[i+3]=words[3];
                    aux[i+4]=words[4];
                    aux[i+5]=words[5];
                    try {
                            strLine = br.readLine();
                    } catch (Exception e) {
                    }
            }
            return aux;
    } else
            return null;

		
	}
}